package com.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blog.base.ThreadLocalUserId;
import com.blog.bean.domain.ChatGptDirectory;
import com.blog.bean.vo.ChatGptDirectoryVO;
import com.blog.exception.ServiceException;
import com.blog.mapper.ChatGptDirectoryMapper;
import com.blog.service.ChatGptDirectoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 任浩
 * @version 1.0
 */
@Service
public class ChatGptDirectoryServiceImpl implements ChatGptDirectoryService {

    @Autowired
    private ChatGptDirectoryMapper chatGptDirectoryMapper;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer insert(ChatGptDirectory chatGptDirectory) {
        return chatGptDirectoryMapper.insert(chatGptDirectory);
    }

    @Override
    public ChatGptDirectory getById(Integer category) {
        return chatGptDirectoryMapper.selectById(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void updateById(ChatGptDirectory chatGptDirectory) {
        chatGptDirectoryMapper.updateById(chatGptDirectory);
    }


    /**
     * 获取聊天记录列表
     * @return
     */
    @Override
    public List<ChatGptDirectoryVO> getList() {
        //根据当前用户获取当前用户的聊天记录列表
        Integer userId = Integer.parseInt(ThreadLocalUserId.get());
        List<ChatGptDirectory> list =chatGptDirectoryMapper.getList(userId);
        List<ChatGptDirectoryVO> res = new ArrayList<>();
        list.forEach(data ->{
            ChatGptDirectoryVO chatGptDirectoryVO = new ChatGptDirectoryVO();
            BeanUtils.copyProperties(data,chatGptDirectoryVO);
            res.add(chatGptDirectoryVO);
        });
        return res;
    }

    /**
     * 搜索聊天记录列表
     * @param title
     * @return
     */
    @Override
    public List<ChatGptDirectoryVO> search(String title) {
        Integer userId = Integer.parseInt(ThreadLocalUserId.get());
        QueryWrapper<ChatGptDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title",title).eq("user_id",userId).eq("del",0).orderByDesc("create_time");
        List<ChatGptDirectory> chatGptDirectories = chatGptDirectoryMapper.selectList(queryWrapper);
        List<ChatGptDirectoryVO> res = new ArrayList<>();
        chatGptDirectories.forEach(data ->{
            ChatGptDirectoryVO chatGptDirectoryVO = new ChatGptDirectoryVO();
            BeanUtils.copyProperties(data,chatGptDirectoryVO);
            res.add(chatGptDirectoryVO);
        });
        return res;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public String deleteById(Integer id) {
        ChatGptDirectory chatGptDirectory = ChatGptDirectory.builder().id(id).del(1).build();
        int num = chatGptDirectoryMapper.updateById(chatGptDirectory);
        if (num == 0) {
            throw new ServiceException("删除目录失败");
        }
        return "删除成功";
    }


}
